for _ in range(int(input())):
total_width, total_height = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
w1 = x2 - x1
h1 = y2 - y1
w2, h2 = map(int, input().split())
ans = float('inf')
left_place = w2
right_place = total_width - w2
top_place = total_height - h2
bottom_place = h2
amount_needed_left = max(left_place - x1, 0)
amount_needed_right = max(x2 - right_place, 0)
amount_needed_top = max(y2 - top_place, 0)
amount_needed_bottom = max(bottom_place - y1, 0)
if w1 + w2 > total_width:
amount_needed_left = float('inf')
amount_needed_right = float('inf')
if h1 + h2 > total_height:
amount_needed_top = float('inf')
amount_needed_bottom = float('inf')
ans = min(amount_needed_left, amount_needed_right, amount_needed_top, amount_needed_bottom);
print(ans if ans != float('inf') else -1)
#include <bits/stdc++.h>
using namespace std;
void solve() {
int W, H, w, h, x1, y1, x2, y2;
cin >> W >> H >> x1 >> y1 >> x2 >> y2 >> w >> h;
int res = INT_MAX;
if (x2 - x1 + w <= W) { res = min(res, max(0, w - x1)); res = min(res, max(0, x2 - (W - w))); }
if (y2 - y1 + h <= H) { res = min(res, max(0, h - y1)); res = min(res, max(0, y2 - (H - h))); }
if (res == INT_MAX) cout << -1 << "\n";
else cout << (double) (res) << "\n"; return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T; cout << fixed << setprecision(9);
for (int t = 1; t <= T; t++) solve();
return 0;
}
1671C - Dolce Vita | 1669G - Fall Down |
4D - Mysterious Present | 1316B - String Modification |
1204A - BowWow and the Timetable | 508B - Anton and currency you all know |
1672A - Log Chopping | 300A - Array |
48D - Permutations | 677C - Vanya and Label |
1583B - Omkar and Heavenly Tree | 1703C - Cypher |
1511C - Yet Another Card Deck | 1698A - XOR Mixup |
1702E - Split Into Two Sets | 1703B - ICPC Balloons |
1702F - Equate Multisets | 1700A - Optimal Path |
665C - Simple Strings | 1708A - Difference Operations |
1703E - Mirror Grid | 1042A - Benches |
1676B - Equal Candies | 1705B - Mark the Dust Sweeper |
1711A - Perfect Permutation | 1701B - Permutation |
1692A - Marathon | 1066A - Vova and Train |
169B - Replacing Digits | 171D - Broken checker |